package com.zhoutao.blog.seven.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhoutao.blog.seven.dao.mapper.TagMapper;
import com.zhoutao.blog.seven.dao.model.TagDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Optional;

@Slf4j
@Repository
public class TagRepository extends ServiceImpl<TagMapper, TagDao> {
    public Optional<TagDao> findByName(String tag) {
        if (!StringUtils.hasText(tag)) {
            return Optional.empty();
        }

        LambdaQueryWrapper<TagDao> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TagDao::getName, tag);
        wrapper.orderByDesc(TagDao::getId);
        wrapper.last("limit 1");
        return Optional.ofNullable(this.getOne(wrapper));
    }
}
